<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="key">
        <el-input
          v-model="queryParams.label"
          placeholder="请输入楼栋/楼层/寝室"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['stuManage:dor:add']"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading" :data="dorList" @selection-change="handleSelectionChange"
      style="width: 100%"
      row-key="id"
      border
      lazy
      :load="load"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column
        prop="label"
        label="校区/楼栋/楼层/寝室/床位">
      </el-table-column>
      <el-table-column label="学生人数" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.type===2 || scope.row.type===3 || scope.row.type===4 ">{{scope.row.buildingStuNum || scope.row.floorStuNum || scope.row.roomStuNum}}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否隔离寝室" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type===4 && scope.row.isIsolation===1" type="danger">是</el-tag>
          <el-tag v-if="scope.row.type===4 && scope.row.isIsolation===0">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="学生姓名" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">
          <span>{{scope.row.stuName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否入住" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type===5 && scope.row.isOccupy===1" type="danger">是</el-tag>
          <el-tag v-if="scope.row.type===5 && scope.row.isOccupy===0">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="110">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.type!==1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-if="scope.row.type!==1"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--<pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />-->

    <!-- 添加或修改寝室对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="200px">
        <el-form-item label="校区/楼栋/楼层/房间号" prop="areaBuildingFloorRoomBedNo">
          <el-cascader
            style="width: 260px"
            v-model="form.areaBuildingFloorRoomBedNo"
            :options="stuDorCasList"
            :props="casProps"
            placeholder="试试搜索:A6"
            filterable
            @change="handleChange"></el-cascader>
        </el-form-item>
        <el-form-item :label="label" prop="newValue">
          <el-input v-model="form.newValue"></el-input>
        </el-form-item>
        <el-form-item v-if="form.type===4" label="隔离寝室" prop="isIsolation">
          <el-radio v-model="form.isIsolation" :label="1">是</el-radio>
          <el-radio v-model="form.isIsolation" :label="0">否</el-radio>
        </el-form-item>
        <el-form-item v-if="form.type===5" label="已入住" prop="isOccupy">
          <el-radio v-model="form.isOccupy" :label="1">是</el-radio>
          <el-radio v-model="form.isOccupy" :label="0">否</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listDor, getDor, delDor, addDor, updateDor, findAreaBuildingFloorRoom,findChildrenDorCasById} from "@/api/stu/dor";

  export default {
    name: "Dor",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 寝室表格数据
        dorList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          parentId: null,
          schoolArea: null,
          label: null,
          value: null,
          isIsolation: null,
        },
        // 表单参数
        form: {
          areaBuildingFloorRoomBedNo: [],
          buildingNo: '',
          floorNo: '',
          roomNo: '',
          bedNo: '',
          isIsolation: null
        },
        // 表单校验
        rules: {},
        stuDorCasList: [],
        stuDorCasUpdateList: [],
        casProps: {
          label: 'label',
          value: 'id',
          checkStrictly: true
        },
        defaultProps: {
          label: 'label',
          value: 'id',
        },
        label: '',
        schoolArea: null,
      };
    },
    created() {
      this.getList()
      //校区楼栋楼层房间 4级级联菜单
      this.getAreaBuildingFloorRoomList();
      this.getAreaBuildingFloorRoomBedList();
    },
    methods: {
      handleChange() {
        this.labelDor(this.form.areaBuildingFloorRoomBedNo.length + 1)
      },
      load(tree, treeNode, resolve) {
        this.loading = true;
        listDor({parentId:tree.id}).then(res => {
          resolve(res.rows)
          this.loading = false;
        })
      },
      /** 查询寝室列表 */
      getList() {
        this.loading = true;
        listDor({parentId:0}).then(response => {
          this.dorList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      getAreaBuildingFloorRoomList() {
        findAreaBuildingFloorRoom().then(res => {
          this.stuDorCasList = res.data
        })
      },
      getAreaBuildingFloorRoomBedList(){
        findChildrenDorCasById({parentId:0}).then(res=>{
          this.stuDorCasUpdateList=res.data
        })
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          parentId: null,
          schoolArea: null,
          label: null,
          value: null,
          isIsolation: null,
          areaBuildingFloorRoomBedNo: []
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.loading = true;
        listDor({label:this.queryParams.label}).then(response => {
          this.dorList = response.rows;
          this.loading = false;
        });
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.$tab.refreshPage()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.stuDorCasList=this.stuDorCasUpdateList
        this.reset();
        const id = row.id || this.ids
        getDor(id).then(response => {
          this.form = response.data;
          this.labelDor(this.form.type)
          this.open = true;
          this.title = "修改";
        });
      },
      labelDor(type) {
        if (type === 2) this.label = '楼栋号'
        if (type === 3) this.label = '楼层号'
        if (type === 4) this.label = '房间号'
        if (type === 5) this.label = '床位号'
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateDor(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList()
                // 刷新当前页签
                this.$tab.refreshPage();
              });
            } else {
              addDor(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.$tab.refreshPage();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('确认删除【' + row.label + '】？').then(function () {
          return delDor(ids);
        }).then(() => {
          this.$modal.msgSuccess("删除成功");
          this.queryParams.parentId = row.parentId
          // 刷新当前页签
          this.$tab.refreshPage();
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('stuManage/dor/export', {
          ...this.queryParams
        }, `dor_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
